#define F_CPU 8000000L
#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
static void delay(uint16_t us)
{
	while ( us ) us--;
}
int main(void)
{
	uint16_t i = 0;
	uint8_t j = 0;
	
	DDRB = 0xff;				// use all pins on PortB for output
	PORTB = 0x00;				// set output high -> turn all LEDs off
	DDRD |= _BV(PD6);			//set PD6 as an output
	DDRD |= _BV(PD5);			//set PD5 as an output
	DDRD |= _BV(PD3);			//set PD3 as an output
	TCCR1A = 0xC2;				//fast pwm mode, ICR1 = top
	TCCR1B = 0x19;				//prescaler = 1
	
	ICR1 = 0xFF;				//load top
	OCR1A =ICR1/2;				//start with a 50% duty cycle
	
	// timer 0
	TCCR0A = 0xF3;				//fast pwm mode OCR0A = Top
	TCCR0B = 0x09;				// prescaler = 1, fast pwm
	OCR0A = 0xFF;				//load top value
	OCR0B = 0xFF;				// start at 0% duty cycle
	
	
	//timer 2 - 
	TCCR2A = 0xF3;				//fast pwm mode OCR2A = Top
	TCCR2B = 0x09;				// prescaler = 1, fast pwm
	OCR2A = 0xFF;				//load top value
	OCR2B = 0xFF;				//start at 0% duty cyle
	
	OCR1A =  0xFF;
	
	for (;;){
	
		OCR0A = 0xFF;			//turn off red
		ICR1 = 0xFF;			//turn off green
		OCR2A = 0xFF;			//turn off blue
		OCR0B = 0xFF;			//turn off red
		OCR1A = 0xFF;			//turn off green
		OCR2B = 0xFF;			//turn off blue
	
		i = 255;	
		j = 254;
		// Fade in Green 
		while(i > 0)
		{
			OCR1A = i;
			i= i - (255*.05);	//updates i value
			_delay_ms(80);
		}
		
		i = 255*.05;
		_delay_ms(250);			// hold green
		
		while(i < 255)
		{
			OCR1A = i;
			i= i + (255*.05);	// Fade out Green
			OCR0B = j;
			j = 255-i;			//fade in red
			if(j < 0)
				j = 0;
			_delay_ms(80);
		}
	
		// turn Green off;
		while(OCR1A < 255)
		{
			OCR1A++;			 // fades out green completely;
			_delay_ms(80);
		}
		
		_delay_ms(250);			// hold red
		
		i = 255;
		j = 0;	
		// Fade in blue
		while(i > 0)
		{
			OCR2B = i;
			i= i - (255*.05);	//fade in blue	
			OCR0B = j;
			j = j+(255*.05);	//fade out red
			_delay_ms(80);
		}
		
		while(OCR0B < 254)
		{
			OCR0B++;			// fades out red completely;
			_delay_ms(80);
		}
		
		 _delay_ms(250);		// hold blue
		
		i = OCR2B;
		while(i < 255)
		{
			OCR2B = i;			//Fade out blue
			i = i + 255*.05;
			_delay_ms(80);
		}
	
		// below is code to produce a sequence for a "pastel rainbow"
		/*
		
		//light red
		while(1){
		OCR0B = 13;
		OCR1A = 147;
		OCR2B = 176;
		_delay_ms(200);
		
		OCR0B = 9;
		OCR1A = 113;
		OCR2B = 168;
		_delay_ms(200);
		
		OCR0B = 4;
		OCR1A = 80;
		OCR2B = 162;
		_delay_ms(200);
		
		OCR0B = 0;
		OCR1A = 10;
		OCR2B = 151;
		_delay_ms(200);
		
		OCR0B = 83;
		OCR1A = 44;
		OCR2B = 140;
		_delay_ms(200);
		
		OCR0B = 121;
		OCR1A = 58;
		OCR2B = 135;
		_delay_ms(200);
		
		OCR0B = 195;
		OCR1A = 71;
		OCR2B = 75;
		_delay_ms(200);
		
		OCR0B = 255;
		OCR1A = 64;
		OCR2B = 12;
		_delay_ms(200);
		
		OCR0B = 187;
		OCR1A = 115;
		OCR2B = 52;
		_delay_ms(200);
		
		OCR0B = 169;
		OCR1A = 139;
		OCR2B = 70;
		_delay_ms(200);
		
		OCR0B = 159;
		OCR1A = 163;
		OCR2B = 87;
		_delay_ms(200);
		
		OCR0B = 122;
		OCR1A = 159;
		OCR2B = 87;
		_delay_ms(200);
		
		OCR0B = 87;
		OCR1A = 155;
		OCR2B = 87;
		_delay_ms(200);
		
		OCR0B = 15;
		OCR1A = 145;
		OCR2B = 85;
		_delay_ms(200);
		} */
	}
	return 0;
}